package net.cl;

import java.util.Enumeration;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Properties;

import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import static java.lang.System.out;

public class Printer {

	@SuppressWarnings("rawtypes")
	public static void print(final Map map) {
		for (Iterator iterator = map.entrySet().iterator(); iterator.hasNext();) {
			Entry entry = (Entry) iterator.next();
			out.println(entry.getKey() + "=" + entry.getValue());
		}
	}

	@SuppressWarnings("rawtypes")
	public static void print(final Properties prop) {
		for (Iterator iterator = prop.entrySet().iterator(); iterator.hasNext();) {
			Entry entry = (Entry) iterator.next();
			out.println(entry.getKey() + "=" + entry.getValue());
		}
	}

	public static void print(final Enumeration<?> e) {
		while (e.hasMoreElements())
			out.println(e.nextElement());
	}

	public static void print(Node n) {
		out.println(renderDom(n));
	}

	private static String renderDom(Node n) {
		StringBuilder sb = new StringBuilder();
		if (n.getNodeType() == Node.TEXT_NODE) {
			sb.append(n.getTextContent().trim());
		} else {
			NamedNodeMap nnm = n.getAttributes();
			sb.append("<").append(n.getNodeName());
			for (int i = 0; nnm != null && i < nnm.getLength(); i++) {
				sb.append(" ").append(nnm.item(i).getNodeName()).append("=").append("\"")
						.append(nnm.item(i).getNodeValue()).append("\"");
			}
			sb.append(">");
		}
		NodeList children = n.getChildNodes();
		int length = children.getLength();
		for (int i = 0; i < length; i++) {
			sb.append(renderDom(children.item(i)));
		}
		if (n.getNodeType() != Node.TEXT_NODE) {
			sb.append("\n").append("</").append(n.getNodeName()).append(">");
		}
		return sb.toString();
	}

}
